package com.cutefool.zero.code.core.domain;

import com.cutefool.dsl.mybatis.core.objects.BasicsDomain;
import com.fasterxml.jackson.annotation.JsonAlias;
import jakarta.annotation.Generated;
import java.sql.JDBCType;
import java.time.LocalDateTime;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Objects;
import java.util.function.UnaryOperator;
import lombok.Data;
import lombok.EqualsAndHashCode;
import org.mybatis.dynamic.sql.SqlBuilder;
import org.mybatis.dynamic.sql.SqlColumn;
import org.mybatis.dynamic.sql.insert.InsertDSL;
import org.mybatis.dynamic.sql.insert.MultiRowInsertDSL;
import org.mybatis.dynamic.sql.update.UpdateDSL;
import org.mybatis.dynamic.sql.update.UpdateModel;

@Data
@EqualsAndHashCode(callSuper = true)
public class Model extends BasicsDomain<Model> {
    @Generated("org.mybatis.generator.api.MyBatisGenerator")
    @JsonAlias("NAME_")
    private String name;

    @Generated("org.mybatis.generator.api.MyBatisGenerator")
    @JsonAlias("TABLE_NAME_")
    private String tableName;

    @Generated("org.mybatis.generator.api.MyBatisGenerator")
    @JsonAlias("TITLE_")
    private String title;

    @Generated("org.mybatis.generator.api.MyBatisGenerator")
    @JsonAlias("TITLE_TEMPLATE")
    private String titleTemplate;

    @Generated("org.mybatis.generator.api.MyBatisGenerator")
    @JsonAlias("IS_SYNC")
    private Byte isSync;

    @Generated("org.mybatis.generator.api.MyBatisGenerator")
    @JsonAlias("DESCRIPTION_")
    private String description;

    @Generated("org.mybatis.generator.api.MyBatisGenerator")
    @JsonAlias("RECORD_TIME")
    private Byte recordTime;

    @Generated("org.mybatis.generator.api.MyBatisGenerator")
    @JsonAlias("RECORD_OPERATOR")
    private Byte recordOperator;

    @Generated("org.mybatis.generator.api.MyBatisGenerator")
    @JsonAlias("SOFT_DELETE")
    private Byte softDelete;

    @Generated("org.mybatis.generator.api.MyBatisGenerator")
    @JsonAlias("DBMS_ID_")
    private Long dbmsId;

    @Generated("org.mybatis.generator.api.MyBatisGenerator")
    @JsonAlias("APP_ID_")
    private Long appId;

    @Generated("org.mybatis.generator.api.MyBatisGenerator")
    @JsonAlias("COMPANY_ID_")
    private Long companyId;

    private static final Model instance = new Model();

    private static final Table table = new Table();

    public static Model instance() {
        return instance;
    }

    @Override
    public Table table() {
        return table;
    }

    @Override
    @SuppressWarnings("DuplicatedCode")
    public UnaryOperator<InsertDSL<Model>> insert() {
        return (InsertDSL<Model> dsl) -> {
            dsl.map(table.id).toProperty("id");
            dsl.map(table.name).toProperty("name");
            dsl.map(table.tableName).toProperty("tableName");
            dsl.map(table.title).toProperty("title");
            dsl.map(table.titleTemplate).toProperty("titleTemplate");
            dsl.map(table.isSync).toProperty("isSync");
            dsl.map(table.description).toProperty("description");
            dsl.map(table.recordTime).toProperty("recordTime");
            dsl.map(table.recordOperator).toProperty("recordOperator");
            dsl.map(table.softDelete).toProperty("softDelete");
            dsl.map(table.dbmsId).toProperty("dbmsId");
            dsl.map(table.appId).toProperty("appId");
            dsl.map(table.createBy).toProperty("createBy");
            dsl.map(table.createAt).toProperty("createAt");
            dsl.map(table.updateBy).toProperty("updateBy");
            dsl.map(table.updateAt).toProperty("updateAt");
            dsl.map(table.companyId).toProperty("companyId");
            return dsl;
        };
    }

    @Override
    @SuppressWarnings("DuplicatedCode")
    public UnaryOperator<MultiRowInsertDSL<Model>> insertMultiple() {
        return (MultiRowInsertDSL<Model> dsl) -> {
            dsl.map(table.id).toProperty("id");
            dsl.map(table.name).toProperty("name");
            dsl.map(table.tableName).toProperty("tableName");
            dsl.map(table.title).toProperty("title");
            dsl.map(table.titleTemplate).toProperty("titleTemplate");
            dsl.map(table.isSync).toProperty("isSync");
            dsl.map(table.description).toProperty("description");
            dsl.map(table.recordTime).toProperty("recordTime");
            dsl.map(table.recordOperator).toProperty("recordOperator");
            dsl.map(table.softDelete).toProperty("softDelete");
            dsl.map(table.dbmsId).toProperty("dbmsId");
            dsl.map(table.appId).toProperty("appId");
            dsl.map(table.createBy).toProperty("createBy");
            dsl.map(table.createAt).toProperty("createAt");
            dsl.map(table.updateBy).toProperty("updateBy");
            dsl.map(table.updateAt).toProperty("updateAt");
            dsl.map(table.companyId).toProperty("companyId");
            return dsl;
        };
    }

    @Override
    public void updateAllColumns(UpdateDSL<UpdateModel> dsl) {
        dsl.set(table.name).equalTo(this::getName)
           .set(table.tableName).equalTo(this::getTableName)
           .set(table.title).equalTo(this::getTitle)
           .set(table.titleTemplate).equalTo(this::getTitleTemplate)
           .set(table.isSync).equalTo(this::getIsSync)
           .set(table.description).equalTo(this::getDescription)
           .set(table.recordTime).equalTo(this::getRecordTime)
           .set(table.recordOperator).equalTo(this::getRecordOperator)
           .set(table.softDelete).equalTo(this::getSoftDelete)
           .set(table.dbmsId).equalTo(this::getDbmsId)
           .set(table.appId).equalTo(this::getAppId)
           .set(table.createBy).equalTo(this::getCreateBy)
           .set(table.createAt).equalTo(this::getCreateAt)
           .set(table.updateBy).equalTo(this::getUpdateBy)
           .set(table.updateAt).equalTo(this::getUpdateAt)
           .set(table.companyId).equalTo(this::getCompanyId);
        if(Objects.nonNull(getId())) {
            dsl.where(table.id, SqlBuilder.isEqualTo(this::getId));
        }
    }

    @Override
    public void updateSelectiveColumns(UpdateDSL<UpdateModel> dsl) {
        dsl.set(table.name).equalToWhenPresent(this::getName)
           .set(table.tableName).equalToWhenPresent(this::getTableName)
           .set(table.title).equalToWhenPresent(this::getTitle)
           .set(table.titleTemplate).equalToWhenPresent(this::getTitleTemplate)
           .set(table.isSync).equalToWhenPresent(this::getIsSync)
           .set(table.description).equalToWhenPresent(this::getDescription)
           .set(table.recordTime).equalToWhenPresent(this::getRecordTime)
           .set(table.recordOperator).equalToWhenPresent(this::getRecordOperator)
           .set(table.softDelete).equalToWhenPresent(this::getSoftDelete)
           .set(table.dbmsId).equalToWhenPresent(this::getDbmsId)
           .set(table.appId).equalToWhenPresent(this::getAppId)
           .set(table.createBy).equalToWhenPresent(this::getCreateBy)
           .set(table.createAt).equalToWhenPresent(this::getCreateAt)
           .set(table.updateBy).equalToWhenPresent(this::getUpdateBy)
           .set(table.updateAt).equalToWhenPresent(this::getUpdateAt)
           .set(table.companyId).equalToWhenPresent(this::getCompanyId);
        if(Objects.nonNull(getId())) {
            dsl.where(table.id, SqlBuilder.isEqualTo(this::getId));
        }
    }

    @Override
    @SuppressWarnings("all")
    public SqlColumn[] columns() {
        return new SqlColumn[]{table.id, table.name, table.tableName, table.title, table.titleTemplate, table.isSync, table.description, table.recordTime, table.recordOperator, table.softDelete, table.dbmsId, table.appId, table.createBy, table.createAt, table.updateBy, table.updateAt, table.companyId};
    }

    @Override
    public Map<String, SqlColumn<?>> columnsMap() {
        Map<String, SqlColumn<?>> maps = new LinkedHashMap<>(17);
        maps.put("id", table.id);
        maps.put("name", table.name);
        maps.put("tableName", table.tableName);
        maps.put("title", table.title);
        maps.put("titleTemplate", table.titleTemplate);
        maps.put("isSync", table.isSync);
        maps.put("description", table.description);
        maps.put("recordTime", table.recordTime);
        maps.put("recordOperator", table.recordOperator);
        maps.put("softDelete", table.softDelete);
        maps.put("dbmsId", table.dbmsId);
        maps.put("appId", table.appId);
        maps.put("createBy", table.createBy);
        maps.put("createAt", table.createAt);
        maps.put("updateBy", table.updateBy);
        maps.put("updateAt", table.updateAt);
        maps.put("companyId", table.companyId);
        return maps;
    }

    @Override
    public SqlColumn<Long> id() {
        return table.id;
    }

    public static final class Table extends org.mybatis.dynamic.sql.AliasableSqlTable<Table> {
        public final SqlColumn<Long> id = column("ID_", JDBCType.BIGINT);

        public final SqlColumn<String> name = column("NAME_", JDBCType.VARCHAR);

        public final SqlColumn<String> tableName = column("TABLE_NAME_", JDBCType.VARCHAR);

        public final SqlColumn<String> title = column("TITLE_", JDBCType.VARCHAR);

        public final SqlColumn<String> titleTemplate = column("TITLE_TEMPLATE", JDBCType.VARCHAR);

        public final SqlColumn<Byte> isSync = column("IS_SYNC", JDBCType.TINYINT);

        public final SqlColumn<String> description = column("DESCRIPTION_", JDBCType.VARCHAR);

        public final SqlColumn<Byte> recordTime = column("RECORD_TIME", JDBCType.TINYINT);

        public final SqlColumn<Byte> recordOperator = column("RECORD_OPERATOR", JDBCType.TINYINT);

        public final SqlColumn<Byte> softDelete = column("SOFT_DELETE", JDBCType.TINYINT);

        public final SqlColumn<Long> dbmsId = column("DBMS_ID_", JDBCType.BIGINT);

        public final SqlColumn<Long> appId = column("APP_ID_", JDBCType.BIGINT);

        public final SqlColumn<Long> createBy = column("CREATE_BY_", JDBCType.BIGINT);

        public final SqlColumn<LocalDateTime> createAt = column("CREATE_AT_", JDBCType.TIMESTAMP);

        public final SqlColumn<Long> updateBy = column("UPDATE_BY_", JDBCType.BIGINT);

        public final SqlColumn<LocalDateTime> updateAt = column("UPDATE_AT_", JDBCType.TIMESTAMP);

        public final SqlColumn<Long> companyId = column("COMPANY_ID_", JDBCType.BIGINT);

        public Table() {
            super("CUTE_MODEL", Table::new);
        }
    }
}